import { NCalendar } from "naive-ui";
import type { CalendarProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type CalendarFactoryConstructorType = {
  props?: CalendarProps;
  attrs?: HTMLAttributes;
  defaultSlot?: (props: {
    year: number;
    month: number;
    date: number;
  }) => BaseComponentType;
  headerSlot?: (props: { year: number; month: number }) => BaseComponentType;
};

export class CalendarFactory {
  private props: CalendarProps = {};
  private attrs: HTMLAttributes = {};
  private defaultSlot: (props: {
    year: number;
    month: number;
    date: number;
  }) => BaseComponentType = () => null;
  private headerSlot: (props: {
    year: number;
    month: number;
  }) => BaseComponentType = () => null;
  constructor(data?: CalendarFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.headerSlot) this.setHeader(data.headerSlot);
  }
  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: CalendarProps) {
    this.props = props;
    return this;
  }
  setDefault(
    component: (props: {
      year: number;
      month: number;
      date: number;
    }) => BaseComponentType
  ) {
    this.defaultSlot = component;
    return this;
  }
  setHeader(
    component: (props: { year: number; month: number }) => BaseComponentType
  ) {
    this.headerSlot = component;
    return this;
  }

  create() {
    return (
      <NCalendar {...this.attrs} {...this.props}>
        {{
          default: (props: { year: number; month: number; date: number }) =>
            getSlotsDom(this.defaultSlot(props)),
          header: (props: { year: number; month: number }) =>
            getSlotsDom(this.headerSlot(props)),
        }}
      </NCalendar>
    );
  }
}
